/* Tables of covariate balance for phases I-V
For phase V, only include treatments 1-4 since everyone saw 5 or 6.
*/

clear
set more off

*Set working directory to rd graph folder:
cd "${main}"

cap log close
log using "${logs}/tables_covariate_balance.log", replace

/*=================================
Check covariate balance
=======================================*/

cap program drop var_prep
program define var_prep


****Balance of covariates***
gen female = playergender == 1
* College stats: College level or Graduate level
gen grad_stats = playerstats_knowledge == 4
* College education: bachelors, masters, PhD
gen college_ed = playereducation == 5 | playereducation == 6 | playereducation == 7
gen age18_22 = playerage == 1
gen age23_49 = playerage == 2 | playerage == 3
gen age50_old = playerage == 4 | playerage == 5
destring playerprofession, replace
gen student = playerprofession == 1 | playerprofession == 2

cap gen freshplayer = (repeat_participant == 2)

*Variable labels		
label variable female "Female"		
label variable college_ed "Completed college"
label variable age18_22 "Age 18 to 22"
label variable age23_49 "Age 23 to 49"
label variable age50_old "Age 50 or older"
label variable grad_stats "Graduate stats knowledge"
label variable student "Current student"
label variable attention "Passed attention check"
cap label variable freshplayer "First time player"

end 


cap file close t1
capture erase "${output}/covariate_balI.tex"
	file open t1 using "${output}/covariate_balI.tex", write replace

		file write t1 ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c c } "_n ///
		" &\multicolumn{5}{c}{} \\"_n ///
		" &    T1 &  T2  & T3 & T4 & p-value   \\"_n ///
		"\cline{2-6} \\" _n ///


/*==============================================================================
Phase I Table
-----------------------------------*/
				

use "${dat}/Pilot 1.0/survey_merged.dta", clear
cap drop _merge

var_prep
	
foreach var of varlist female college_ed age18_22 age23_49 age50_old grad_stats attention {

reg `var' treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_`var' = string(r(p), "%10.3fc")

forvalues x = 1/4{
su `var' if treat`x' == 1
local mean_`var'`x' = string(r(mean),  "%10.3fc")
}

file write t1 "`: variable label `var''   & `mean_`var'1' & `mean_`var'2' & `mean_`var'3' & `mean_`var'4' & `pval_`var''  \\"_n

}

* Attrition rate
* Compute attrition rate. Max possible responses per arm: 968
* Create "dummy" observations for people who dropped out
gen fake_data = 0
gen attrition = 0
* With no attrition, have 968 * 4 = 3872 observations
qui count
local n_real = r(N)
set obs 3872

replace fake_data = 1 if fake_data == .
replace attrition = 1 if attrition == .

* Add appropriate number of attriters for each treatment group
gen temp = ceil((_n - `n_real') / 11)
count if playertreatment_group == "A"
local toadd = (968 - r(N)) / 11
local nA = 968 - r(N)
replace playertreatment_group = "A" if fake_data == 1 & temp <= `toadd'
replace temp = temp - `toadd'

count if playertreatment_group == "B"
local toadd = (968 - r(N)) / 11
local nB = r(N)
replace playertreatment_group = "B" if fake_data == 1 & temp <= `toadd' & temp > 0
replace temp = temp - `toadd'

count if playertreatment_group == "C"
local toadd = (968 - r(N)) / 11
local nC = r(N)
replace playertreatment_group = "C" if fake_data == 1 & temp <= `toadd' & temp > 0

replace playertreatment_group = "D" if playertreatment_group == ""

replace pid = string(ceil((_n - `n_real') / 11)) if fake_data == 1

label var attrition "Attrition rate"
replace treat1 = playertreatment_group == "A"
replace treat2 = playertreatment_group == "B"
replace treat3 = playertreatment_group == "C"
replace treat4 = playertreatment_group == "D"

reg attrition treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_attrition = string(r(p), "%10.3fc")
forvalues x = 1/4{
su attrition if treat`x' == 1
local mean_attrition`x' = string(r(mean),  "%10.3fc")
}

file write t1 "`: variable label attrition'   & `mean_attrition1' & `mean_attrition2' & `mean_attrition3' & `mean_attrition4' & `pval_attrition'  \\"_n


keep if fake_data == 0
count if playertreatment_group == "A"
local nA = r(N) / 11
count if playertreatment_group == "B"
local nB = r(N) / 11
count if playertreatment_group == "C"
local nC = r(N) / 11
count if playertreatment_group == "D"
local nD = r(N) / 11

file write t1 "Participants & `nA' & `nB' & `nC' & `nD' & \\"_n

file write t1 "\cline{1-6} \\" _n

file write t1 "\end{tabular*} "_n ///
	"}"_n
	
file close t1
	
	


/*==============================================================================
Phase II Table
-----------------------------------*/

cap file close t2
capture erase "${output}/covariate_balII.tex"
	file open t2 using "${output}/covariate_balII.tex", write replace

	
		file write t2 ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c c } "_n ///
		" &\multicolumn{5}{c}{} \\"_n ///
		" &    T1 &  T2  & T3 & T4 & p-value   \\"_n ///
		"\cline{2-6} \\" _n ///




	
use "${dat}/Phase II/survey_merged_PhaseII.dta", clear

var_prep


foreach var of varlist female college_ed age18_22 age23_49 age50_old grad_stats attention freshplayer  {

reg `var' treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_`var' = string(r(p), "%10.3fc")


forvalues x = 1/4{
su `var' if treat`x' == 1
local mean_`var'`x' = string(r(mean),  "%10.3fc")
}



file write t2 "`: variable label `var''   & `mean_`var'1' & `mean_`var'2' & `mean_`var'3' & `mean_`var'4' & `pval_`var''  \\"_n

}

* Attrition rate
* Compute attrition rate. Max possible responses per arm: 968
* Create "dummy" observations for people who dropped out
gen fake_data = 0
gen attrition = 0
* With no attrition, have 968 * 4 = 3872 observations
qui count
local n_real = r(N)
set obs 3872

replace fake_data = 1 if fake_data == .
replace attrition = 1 if attrition == .

* Add appropriate number of attriters for each treatment group
gen temp = ceil((_n - `n_real') / 11)
count if playertreatment_group == "A"
local toadd = (968 - r(N)) / 11
local nA = 968 - r(N)
replace playertreatment_group = "A" if fake_data == 1 & temp <= `toadd'
replace temp = temp - `toadd'

count if playertreatment_group == "B"
local toadd = (968 - r(N)) / 11
local nB = r(N)
replace playertreatment_group = "B" if fake_data == 1 & temp <= `toadd' & temp > 0
replace temp = temp - `toadd'

count if playertreatment_group == "C"
local toadd = (968 - r(N)) / 11
local nC = r(N)
replace playertreatment_group = "C" if fake_data == 1 & temp <= `toadd' & temp > 0

replace playertreatment_group = "D" if playertreatment_group == ""

replace pid = string(ceil((_n - `n_real') / 11)) if fake_data == 1

label var attrition "Attrition rate"
replace treat1 = playertreatment_group == "A"
replace treat2 = playertreatment_group == "B"
replace treat3 = playertreatment_group == "C"
replace treat4 = playertreatment_group == "D"

reg attrition treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_attrition = string(r(p), "%10.3fc")
forvalues x = 1/4{
su attrition if treat`x' == 1
local mean_attrition`x' = string(r(mean),  "%10.3fc")
}

file write t2 "`: variable label attrition'   & `mean_attrition1' & `mean_attrition2' & `mean_attrition3' & `mean_attrition4' & `pval_attrition'  \\"_n

keep if fake_data == 0
count if playertreatment_group == "A"
local nA = r(N) / 11
count if playertreatment_group == "B"
local nB = r(N) / 11
count if playertreatment_group == "C"
local nC = r(N) / 11
count if playertreatment_group == "D"
local nD = r(N) / 11

file write t2 "Participants & `nA' & `nB' & `nC' & `nD' & \\"_n
	
file write t2 "\cline{1-6} \\" _n

file write t2 "\end{tabular*} "_n ///
	"}"_n
	
file close t2


/*==============================================================================
Phase III
-------------------------------*/
cap file close t3
capture erase "${output}/covariate_balIII.tex"
	file open t3 using "${output}/covariate_balIII.tex", write replace

	
		file write t3 ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c } "_n ///
		" &\multicolumn{4}{c}{} \\"_n ///
		" &    T1 &  T2  & T3 &  p-value   \\"_n ///
		"\cline{2-5} \\" _n ///




*----
use "${dat}/Phase III/survey_merged_PhaseIII.dta", clear

var_prep

foreach var of varlist female college_ed age18_22 age23_49 age50_old grad_stats attention freshplayer  {


reg `var' treat2 treat3, vce(cluster pid)
test treat2 treat3
local pval_`var' = string(r(p), "%10.3fc")


forvalues x = 1/3{
su `var' if treat`x' == 1
local mean_`var'`x' = string(r(mean),  "%10.3fc")
}



file write t3 "`: variable label `var''   & `mean_`var'1' & `mean_`var'2' & `mean_`var'3' &  `pval_`var''  \\"_n

}


* Attrition rate
* Compute attrition rate. Max possible responses per arm: 968
* Create "dummy" observations for people who dropped out
gen fake_data = 0
gen attrition = 0
* With no attrition, have 968 * 4 = 3872 observations
qui count
local n_real = r(N)
set obs 2904

replace fake_data = 1 if fake_data == .
replace attrition = 1 if attrition == .

* Add appropriate number of attriters for each treatment group
gen temp = ceil((_n - `n_real') / 11)
count if playertreatment_group == "A"
local toadd = (968 - r(N)) / 11
local nA = 968 - r(N)
replace playertreatment_group = "A" if fake_data == 1 & temp <= `toadd'
replace temp = temp - `toadd'

count if playertreatment_group == "B"
local toadd = (968 - r(N)) / 11
local nB = r(N)
replace playertreatment_group = "B" if fake_data == 1 & temp <= `toadd' & temp > 0
replace temp = temp - `toadd'

replace playertreatment_group = "C" if playertreatment_group == ""

replace pid = string(ceil((_n - `n_real') / 11)) if fake_data == 1

label var attrition "Attrition rate"
replace treat1 = playertreatment_group == "A"
replace treat2 = playertreatment_group == "B"
replace treat3 = playertreatment_group == "C"

reg attrition treat2 treat3, vce(cluster pid)
test treat2 treat3
local pval_attrition = string(r(p), "%10.3fc")
forvalues x = 1/3{
su attrition if treat`x' == 1
local mean_attrition`x' = string(r(mean),  "%10.3fc")
}

file write t3 "`: variable label attrition'   & `mean_attrition1' & `mean_attrition2' & `mean_attrition3' & `pval_attrition'  \\"_n

keep if fake_data == 0
count if playertreatment_group == "A"
local nA = r(N) / 11
count if playertreatment_group == "B"
local nB = r(N) / 11
count if playertreatment_group == "C"
local nC = r(N) / 11

file write t3 "Participants & `nA' & `nB' & `nC' & \\"_n

file write t3 "\cline{1-5} \\" _n

file write t3 "\end{tabular*} "_n ///
	"}"_n
	
file close t3

/*==============================================================================
Phase VI Table
-----------------------------------*/

cap file close t6
capture erase "${output}/covariate_balVI.tex"
	file open t6 using "${output}/covariate_balVI.tex", write replace

	
		file write t6 ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c c } "_n ///
		" &\multicolumn{5}{c}{} \\"_n ///
		" &    T1 &  T2  & T3 & T4 & p-value   \\"_n ///
		"\cline{2-6} \\" _n ///




	
*----
use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear
****************************************************

var_prep


foreach var of varlist female college_ed age18_22 age23_49 age50_old grad_stats attention freshplayer  {

reg `var' treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_`var' = string(r(p), "%10.3fc")


forvalues x = 1/4{
su `var' if treat`x' == 1
local mean_`var'`x' = string(r(mean),  "%10.3fc")
}



file write t6 "`: variable label `var''   & `mean_`var'1' & `mean_`var'2' & `mean_`var'3' & `mean_`var'4' & `pval_`var''  \\"_n

}

* Attrition rate
* Compute attrition rate. Max possible responses per arm: 968
* Create "dummy" observations for people who dropped out
gen fake_data = 0
gen attrition = 0
* With no attrition, have 968 * 4 = 3872 observations
qui count
local n_real = r(N)
set obs 3872

replace fake_data = 1 if fake_data == .
replace attrition = 1 if attrition == .

* Add appropriate number of attriters for each treatment group
gen temp = ceil((_n - `n_real') / 11)
count if playertreatment_group == "A"
local toadd = (968 - r(N)) / 11
local nA = 968 - r(N)
replace playertreatment_group = "A" if fake_data == 1 & temp <= `toadd'
replace temp = temp - `toadd'

count if playertreatment_group == "B"
local toadd = (968 - r(N)) / 11
local nB = r(N)
replace playertreatment_group = "B" if fake_data == 1 & temp <= `toadd' & temp > 0
replace temp = temp - `toadd'

count if playertreatment_group == "C"
local toadd = (968 - r(N)) / 11
local nC = r(N)
replace playertreatment_group = "C" if fake_data == 1 & temp <= `toadd' & temp > 0

replace playertreatment_group = "D" if playertreatment_group == ""

replace pid = string(ceil((_n - `n_real') / 11)) if fake_data == 1

label var attrition "Attrition rate"
replace treat1 = playertreatment_group == "A"
replace treat2 = playertreatment_group == "B"
replace treat3 = playertreatment_group == "C"
replace treat4 = playertreatment_group == "D"

reg attrition treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_attrition = string(r(p), "%10.3fc")
forvalues x = 1/4{
su attrition if treat`x' == 1
local mean_attrition`x' = string(r(mean),  "%10.3fc")
}

file write t6 "`: variable label attrition'   & `mean_attrition1' & `mean_attrition2' & `mean_attrition3' & `mean_attrition4' & `pval_attrition'  \\"_n



keep if fake_data == 0
count if playertreatment_group == "A"
local nA = r(N) / 11
count if playertreatment_group == "B"
local nB = r(N) / 11
count if playertreatment_group == "C"
local nC = r(N) / 11
count if playertreatment_group == "D"
local nD = r(N) / 11

file write t6 "Participants & `nA' & `nB' & `nC' & `nD' & \\"_n

file write t6 "\cline{1-6} \\" _n

file write t6 "\end{tabular*} "_n ///
	"}"_n
	
file close t6


/*==============================================================================
Phase VII Table
-----------------------------------*/

cap file close t7
capture erase "${output}/covariate_balVII.tex"
	file open t7 using "${output}/covariate_balVII.tex", write replace

	
		file write t7 ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c c } "_n ///
		" &\multicolumn{5}{c}{} \\"_n ///
		" &    T1 &  T2  & T3 & T4 & p-value   \\"_n ///
		"\cline{2-6} \\" _n ///




	
*----
use "${dat}/Phase VII/survey_merged_PhaseVII.dta", clear
****************************************************

var_prep


foreach var of varlist female college_ed age18_22 age23_49 age50_old grad_stats attention freshplayer  {

reg `var' treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_`var' = string(r(p), "%10.3fc")


forvalues x = 1/4{
su `var' if treat`x' == 1
local mean_`var'`x' = string(r(mean),  "%10.3fc")
}



file write t7 "`: variable label `var''   & `mean_`var'1' & `mean_`var'2' & `mean_`var'3' & `mean_`var'4' & `pval_`var''  \\"_n

}

* Attrition rate
* Compute attrition rate. Max possible responses per arm: 968
* Create "dummy" observations for people who dropped out
gen fake_data = 0
gen attrition = 0
* With no attrition, have 968 * 4 = 3872 observations
qui count
local n_real = r(N)
set obs 3872

replace fake_data = 1 if fake_data == .
replace attrition = 1 if attrition == .

* Add appropriate number of attriters for each treatment group
gen temp = ceil((_n - `n_real') / 11)
count if playertreatment_group == "A"
local toadd = (968 - r(N)) / 11
local nA = 968 - r(N)
replace playertreatment_group = "A" if fake_data == 1 & temp <= `toadd'
replace temp = temp - `toadd'

count if playertreatment_group == "B"
local toadd = (968 - r(N)) / 11
local nB = r(N)
replace playertreatment_group = "B" if fake_data == 1 & temp <= `toadd' & temp > 0
replace temp = temp - `toadd'

count if playertreatment_group == "C"
local toadd = (968 - r(N)) / 11
local nC = r(N)
replace playertreatment_group = "C" if fake_data == 1 & temp <= `toadd' & temp > 0

replace playertreatment_group = "D" if playertreatment_group == ""

replace pid = string(ceil((_n - `n_real') / 11)) if fake_data == 1

label var attrition "Attrition rate"
replace treat1 = playertreatment_group == "A"
replace treat2 = playertreatment_group == "B"
replace treat3 = playertreatment_group == "C"
replace treat4 = playertreatment_group == "D"

reg attrition treat2 treat3 treat4, vce(cluster pid)
test treat2 treat3 treat4
local pval_attrition = string(r(p), "%10.3fc")
forvalues x = 1/4{
su attrition if treat`x' == 1
local mean_attrition`x' = string(r(mean),  "%10.3fc")
}

file write t7 "`: variable label attrition'   & `mean_attrition1' & `mean_attrition2' & `mean_attrition3' & `mean_attrition4' & `pval_attrition'  \\"_n



keep if fake_data == 0
count if playertreatment_group == "A"
local nA = r(N) / 11
count if playertreatment_group == "B"
local nB = r(N) / 11
count if playertreatment_group == "C"
local nC = r(N) / 11
count if playertreatment_group == "D"
local nD = r(N) / 11

file write t7 "Participants & `nA' & `nB' & `nC' & `nD' & \\"_n

file write t7 "\cline{1-6} \\" _n

file write t7 "\end{tabular*} "_n ///
	"}"_n
	
file close t7

log close
